<?php
/**
 * 报名任务管理
 */
namespace Admin\Controller;
use Think\Controller;
use Think\Log;
class PEnterTaskController extends Controller
{
    protected $return;
   
    public function __construct(){
        header("Access-Control-Allow-Origin: *");
        $this->return = array('errcode'=>0, 'errmsg'=>'ok');
        $admin = session('rush_admin');
        if (!$admin) {
        }
    }

    public function index()
    {
        $data['name'] = I('post.name', '', 'trim');
        $data['start'] = I('post.start', '', 'trim');
        $data['end'] = I('post.end', '', 'trim');
        
        $data['page'] = I('post.page', 1, 'intval');
        $data['page_size'] = I('page_size', 10, 'intval');
        Log::write(var_export($data,true), 'data');
        $where = array();
        if ($data['name'] != '') {
            $where['title'] = array('LIKE', '%'.$data['name'].'%');
        }
        $where['is_deleted'] = 0;


        //任务创建时间开始区间
        if ($data['start']) {
            $where['start_time'][] = array('egt', strtotime($data['start']));
        }
        //任务创建时间结束区间
        if ($data['end']) {
            $where['end_time'][] = array('elt', strtotime($data['end']));
        }

        $total = M("PTask")->where($where)->count();
        $page = pagination($total, $data['page_size'] ,$data['page']);
        if ($total) {
            $list = M("PTask")->where($where)->order('id desc')
                  ->limit($page['offset'], $page['limit'])
                  ->select();
            foreach ($list as $k => $v) {
                $list[$k]['add_time'] = date('Y-m-d H:i', $v['add_time']);
                $list[$k]['start_time'] = date('Y-m-d H:i', $v['start_time']);
                $list[$k]['end_time'] = date('Y-m-d H:i', $v['end_time']);
            }
            $res =  array('data'=>$list, 'page'=>$page);
        }else{
            $res = array('data'=>array(), 'page'=>$page);
        }
        return $this->ajaxReturn(array_merge($this->return, $res));
    }

    public function updateStatus()
    {
        $id = I('post.id', 0, 'intval');
        $status = I('post.status');
        if (!in_array($status, array(0, 1))) {
            return $this->ajaxReturn(array('errcode'=>10001, 'errmsg'=>'状态错误'));
        }
        $res = M("PTask")->where(array('id'=>$id))->setField('status', $status);
        if ($res !== false) {
            return $this->ajaxReturn($this->return);
        }else{
            return $this->ajaxReturn(array('errcode'=>10002, 'errmsg'=>'更新状态失败'));
        }

    }

    public function add()
    {
        $data['title'] = I('post.title', '', 'trim');
        $data['url'] = I('post.url', '', 'trim');
        $data['description'] = I('post.description', '', 'trim');
        $data['encourage'] = I('post.encourage', '', 'trim');

        $data['start_time'] = I('post.start_time', '', 'trim');
        $data['end_time'] = I('post.end_time', '', 'trim');

        if (empty($data['title'])) {
            return $this->ajaxReturn(array('errcode'=>10001, 'errmsg'=>'活动名称不能为空'));
        }
        if (empty($data['description'])) {
            return $this->ajaxReturn(array('errcode'=>10003, 'errmsg'=>'活动内容不能为空'));
        }


        $data['start_time'] = strtotime($data['start_time']);
        $data['end_time'] = strtotime($data['end_time']);
        if ($data['start_time'] >= $data['end_time']) {
            return $this->ajaxReturn(array('errcode'=>10008, 'errmsg'=>'兑换时间错误'));
        }

        $data['is_deleted'] = 0;
        $data['enter_count'] = 0;
        $data['finish_count'] = 0;
        $data['add_time'] = time();

        $res = M("PTask")->add($data);
        if ($res) {
            return $this->ajaxReturn(array('errcode'=>0, 'errmsg'=>'新增报名任务成功'));
        }else{
            return $this->ajaxReturn(array('errcode'=>50000, 'errmsg'=>'新增报名任务失败'));
        }
    }
        
    public function edit()
    {
        $id = I('post.id', 0, 'intval');
        $res = M("PTask")->find($id);
        if ($res) {
            $res['start_time'] = date('Y-m-d H:i:s', $res['start_time']);
            $res['end_time'] = date('Y-m-d H:i:s', $res['end_time']);
            return $this->ajaxReturn(array('errcode'=>0, 'errmsg'=>'ok', 'data'=>$res));
        }else{
            return $this->ajaxReturn(array('errcode'=>50000, 'errmsg'=>'查找不到对应的新手福利数据'));
        }
    }

    public function update()
    {
        $data['title'] = I('post.title', '', 'trim');
        $data['url'] = I('post.url', '', 'trim');
        $data['description'] = I('post.description', '', 'trim');
        $data['id'] = I('post.id', 0, 'intval');
        $data['start_time'] = I('post.start_time', '', 'trim');
        $data['end_time'] = I('post.end_time', '', 'trim');
        $data['encourage'] = I('post.encourage', '', 'trim');
        if (empty($data['title'])) {
            return $this->ajaxReturn(array('errcode'=>10001, 'errmsg'=>'报名任务名称不能为空'));
        }
        if (empty($data['description'])) {
            return $this->ajaxReturn(array('errcode'=>10003, 'errmsg'=>'报名任务内容不能为空'));
        }

        $data['start_time'] = strtotime($data['start_time']);
        $data['end_time'] = strtotime($data['end_time']);
        if ($data['start_time'] >= $data['end_time']) {
            return $this->ajaxReturn(array('errcode'=>10008, 'errmsg'=>'报名任务时间错误'));
        }
        $map['id'] = $data['id'];
        $act = M("PTask")->where($map)->find();
        if (!$act) {
            return $this->ajaxReturn(array('errcode'=>20001,'errmsg'=>'要修改的报名活动不存在'));
        }
        unset($map);

        $res = M("PTask")->save($data);
        if ($res !== false) {
            return $this->ajaxReturn(array('errcode'=>0, 'errmsg'=>'修改报名活动成功'));
        }else{
            return $this->ajaxReturn(array('errcode'=>50000, 'errmsg'=>'修改报名活动失败'));
        }
    }

    public function del()
    {
        $id = I('post.id', 0, 'intval');
        $data['is_deleted'] = 1;
        // Log::write($id);
        $res = M("PTask")->where(array('id'=>$id))->save($data);

        if ($res) {
            $this->ajaxReturn($this->return);
        }else{
            $this->ajaxReturn(array('errcode'=>50000, 'errmsg'=>'删除失败'));
        }
    }

    public function userlist(){
        $data['name'] = I('post.name', '', 'trim');
        $data['phone'] = I('post.phone', '', 'trim');
        $data['account'] = I('post.account', '', 'trim');
        $data['task_id'] = I('post.task_id', '', 'trim');

        $data['page'] = I('post.page', 1, 'intval');
        $data['page_size'] = I('page_size', 10, 'intval');
        Log::write(var_export($data,true), 'data');
        $where = array();
        if ($data['name'] != '') {
              $where['user.name'] = array('LIKE', '%'.$data['name'].'%');
        }
        if ($data['account'] != '') {
              $where['user.account'] = array('LIKE', '%'.$data['account'].'%');
        }
        if ($data['phone'] != '') {
                $where['user.phone'] = array('LIKE', '%'.$data['phone'].'%');
        }
        $where['user.is_deleted'] = 0;
        $where['pp.task_id']=$data['task_id'];

        $total = M("p_task_person pp")->join("LEFT JOIN zsf_p_user user on user.id = pp.user_id")->where($where)->count();
        $page = pagination($total, $data['page_size'] ,$data['page']);
        if ($total) {

            $list = M()->table('zsf_p_task_person pp')->field('pp.*,user.name,user.account,user.phone')
                    ->join("LEFT JOIN zsf_p_user user on user.id = pp.user_id")
                    ->where($where)->order('pp.id desc')
              ->limit($page['offset'], $page['limit'])
              ->select();
            foreach ($list as $k => $v) {
                $list[$k]['add_time'] = date('Y-m-d H:i', $v['add_time']);
                if(empty($v['finish_time']) || $v['scope']<1){
                    $list[$k]['finish_time']='未完成任务';
                }
                else{
                    $list[$k]['finish_time'] = date('Y-m-d H:i', $v['finish_time']);
                }
            }
            $res =  array('data'=>$list, 'page'=>$page);
        }else{
            $res = array('data'=>array(), 'page'=>$page);
        }
        return $this->ajaxReturn(array_merge($this->return, $res));
    }

    public function export(){
        ini_set("memory_limit", "218M");
        set_time_limit(0);
        $data['name'] = I('post.name', '', 'trim');
        $data['phone'] = I('post.phone', '', 'trim');
        $data['account'] = I('post.account', '', 'trim');
        $data['task_id'] = I('post.task_id', '', 'trim');

        $data['page'] = I('post.page', 1, 'intval');
        $data['page_size'] = I('page_size', 10, 'intval');
        Log::write(var_export($data,true), 'data');
        $where = array();
        if ($data['name'] != '') {
              $where['user.name'] = array('LIKE', '%'.$data['name'].'%');
        }
        if ($data['account'] != '') {
              $where['user.account'] = array('LIKE', '%'.$data['account'].'%');
        }
        if ($data['phone'] != '') {
                $where['user.phone'] = array('LIKE', '%'.$data['phone'].'%');
        }
        $where['user.is_deleted'] = 0;
        $where['pp.task_id']=$data['task_id'];


        $list = M()->table('zsf_p_task_person pp')->field('pp.*,user.name,user.account,user.phone')
              ->join("LEFT JOIN zsf_p_user user on user.id = pp.user_id")
              ->where($where)->order('pp.id desc')
              ->limit($page['offset'], $page['limit'])
              ->select();


        header("Content-type:text/csv");
        header("Content-Disposition:attachment;filename=报名名单.csv");
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
        header('Expires:0');
        header('Pragma:public');
        echo iconv("UTF-8", "GB2312", "姓名").",";
        echo iconv("UTF-8", "GB2312", "手机号码").",";
        echo iconv("UTF-8", "GB2312", "银行卡").",";
        echo iconv("UTF-8", "GB2312", "报名时间").",";
        echo iconv("UTF-8", "GB2312", "完成任务时间").",";
        echo iconv("UTF-8", "GB2312", "获得积分奖励");

        echo "\n";
        foreach ($list as $v) {
            $list[$k]['add_time'] = date('Y-m-d H:i', $v['add_time']);
            if(empty($v['finish_time'])){
                $list[$k]['finish_time']='未完成任务';
            }
            else{
                $list[$k]['finish_time'] = date('Y-m-d H:i', $v['finish_time']);
            }

            echo iconv("utf-8", "gb2312//IGNORE", $v['name'])."\t,";
            echo $v['phone']."\t,";
            echo $v['account']."\t,";
            echo $list[$k]['add_time']."\t,";
            echo $list[$k]['finish_time']."\t,";
            echo $v['scope']."\t\n";
        }
        exit;

    }

}